package com.kehutong.api.action;

import com.kehutong.api.service.Oauth2Service;
import com.kehutong.common.Authutil.OAuth2Provider;
import com.kehutong.common.DoveClient;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.oauth2.OAuth2Accessor;
import org.coraframework.oauth2.OAuth2ProblemException;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 客户通oauthor2.0授权
 * @author liuzhen
 * http://localhost:8080/oau-kht/authorize?response_type=code&appId=myKey&redirect_uri=http://localhost:8080/oauth/callBack?userId=huangshu&state=xyz
 */
@MvcAction
@ReqMapping("/oau-kht")
public class Oauth2Action {

	private static final Logger logger = LoggerFactory.getLogger(Oauth2Action.class);

	@Inject
	private Oauth2Service oauth2Service;
	@Inject
	private JdbcSession SignJDBCSession;
	@Inject
	private OAuth2Provider oauth2Provider;
	@Inject
	private DoveClient doveClient;

	@ReqMapping("/authorize")
	public void authorization(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("{}=========", this.hashCode());
		oauth2Service.authorization(request, response);
	}

	@ReqMapping("/callback")
	public void tencentCallback(JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) throws Exception {
		oauth2Service.tencentCallback(jsonObject, request, response);
	}

	@ReqMapping("/toKhtV1")
	public void toKhtV1(JSONObject json, HttpServletRequest request, HttpServletResponse response,JSONObject userJson)
			throws IOException, ServletException {
		try {
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location","http://www.baidu.com");
		} catch (Exception e) {

		}
	}

	@ReqMapping("/gettoken")
	public void getAccess_token(String appId, String secret, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("{}=========", 1111);
		oauth2Service.getAccess_token(appId, secret, request, response);
	}


//	@ReqMapping("/flushtoken")
//	public void flushtoken(String appId, String secret, HttpServletRequest request, HttpServletResponse response) throws Exception {
//		oauth2Service.(appId, secret, request, response);
//	}

	@ReqMapping("/getUserInfo")
	public Object getUserInfo(String access_token, String code) throws Exception {
		logger.debug("{}=========", this.hashCode());
		logger.debug("access_token:{}, code:{}", access_token, code);
		try {
			oauth2Provider.getAccessorByAccessToken(access_token);
		} catch (OAuth2ProblemException e){
			return Result.error(1002, "授权已失效");
		}

		OAuth2Accessor acc = oauth2Provider.getAccessorByCode(code);
		String userId = (String)acc.getProperty("user");
		String extContent=(String)acc.getProperty("extContent");
		if (Objects.isBlank(userId)) {
			return Result.unlogin();
		}

		JSONObject json = new JSONObject();
		json.put("userId", userId);

		if(Objects.nonEmpty(extContent)){
			json.put("extContent",extContent);
		}
		return json;
	}



}
